package com.leyou.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.leyou.constant.Result;
import com.leyou.domain.db1entity.TableCell;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author lixin
 * @Date 2018/10/24 10:49
 * @Version 1.0
 * @Description TODO
 **/
public class Utils {

    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String getUUID(int index) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        if (index > 0 && index < uuid.length()) {
            return uuid.substring(0, index);
        } else {
            return uuid;
        }
    }

    public static String dateFormat(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static Date getCurrentDate() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return parseDate(sdf.format(date), "yyyy-MM-dd HH:mm:ss");
    }

    public static Date parseDate(String strDate, String formate) {
        Date date = null;
        try {
            date = new SimpleDateFormat(formate).parse(strDate);
        } catch (Exception ex) {
            // System.err.println(ex.getMessage());
        }
        return date;
    }


    /**
     * @return 验证结果
     * @Author lixin
     * @Date 18:05 2018/10/24 0024
     * @Param [o]
     * @Description 所传对象全都不为空返回false
     **/
    public static boolean isNull(Object... o) {
        for (Object object : o) {
            if (null == object) {
                return true;
            }
        }
        return false;
    }


    /**
     * @return boolean
     * @Author lixin
     * @Date 18:06 2018/10/24 0024
     * @Param [str]
     * @Description 所传对象全都不为空返回false
     **/
    public static boolean isNull(String... str) {
        for (String s : str) {
            if (s == null || "".equals(s)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 得到几天后的时间
     *
     * @param d
     * @param day
     * @return
     */
    public static Date getDateAfter(Date d, int day) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);//+后 -前
        return now.getTime();
    }


    /**
     * @param tableCellList
     * @return com.leyou.domain.db1entity.TableCell
     * @author lixin
     * @date 19:19 2018/11/22 0022
     * @description 获取随机的奖项
     **/
    public static TableCell getPrize(List<TableCell> tableCellList) {

        List<TableCell> prizePool = new ArrayList();

        for (TableCell tableCell : tableCellList) {
            for (int i = 0; i < tableCell.getProbability(); i++) {
                prizePool.add(tableCell);
            }
        }
        Random random = new Random();
        int n = random.nextInt(prizePool.size());
        return prizePool.get(n);
    }


    public static void main(String[] args) {

//        JSONObject jsonObject = new JSONObject();
//        Map<String, Object> body = new HashMap();
//        body.put("userId", "1697440");
//        body.put("cardnum", "110368932");
//        body.put("point", 100);
//        jsonObject.put("body", body);
//
//        System.out.println(jsonObject.toString());
//
//        String result = HttpRequest.
//                sendGet("http://192.168.98.124:8080/leyou-erp-gateway-services/weixin/postOnePointAndSo?json="
//                , jsonObject.toString());
//        System.out.println(result);

        Map map = new HashMap();
        map.put("cardnum", "9302327342");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("body", map);
        String jsonResult = HttpRequest.sendGet("http://192.168.98.124:8080/leyou-erp-gateway-services/pos/getCardPoint?json=", jsonObject.toString());
        map = JSON.parseObject(jsonResult, Map.class);
        map = JSON.parseObject(map.get("body").toString(), Map.class);
        int currentPonts = 0;
        try {
            currentPonts = Integer.parseInt(map.get("point").toString());
            System.out.println(currentPonts);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }



    }


}
